import sys

def createUser(host,rootpass,username,password,logger):
    from cmd import sshCmd

    logger.info('creating user ...')
    cmd='useradd -d /var/www/html/ '+username+' && echo '+password+' | passwd --stdin '+username
    r=sshCmd(host,'root',rootpass,cmd)
    if r == 'ssh failed':
        logger.error('creating user failed!')
        sys.exit(1)
    else:
        logger.debug(r)
        logger.info('creating user succeed!')

def phpmyadminConf(host,rootpass,logger):
    from cmd import scpCmd
    import os,shutil

    logger.info('configuring phpmyadmin ...')
    shutil.copy('../templateconfig/config.inc.php.template','../temp/config.inc.php')
    os.system('sed -i "72c \$cfg[\'blowfish_secret\']=\''+host+'\';" ../temp/config.inc.php')
    os.system('sed -i "95c \$cfg[\'Servers\'][\$i][\'host\']=\''+host+'\';" ../temp/config.inc.php')
    src='../temp/config.inc.php'
    dest='/var/www/html/craneutility/phpmyadmin/'
    r=scpCmd(host,src,dest,rootpass)
    os.remove(src)
    if r== 'scp failed':
        logger.error('configuring phpmyadmin failed!')
        sys.exit(1)
    else:
        logger.debug(r)
        logger.info('configuring phpmyadmin succeed!')

def mysqlAddUser(host,rootpass,username,password,scope,logger):
    import MySQLdb

    logger.info('adding mysql user ...')
    try:
        conn=MySQLdb.connect(host=host,user='root',passwd=rootpass)
    except:
        logger.debug('mysql server %s can\'t be connected!' %(host))
        logger.error('adding mysql user failed!')
        sys.exit(1)
    cursor=conn.cursor()
    if scope==0:
        cursor.execute("grant all privileges on *.* to %s@localhost identified by '%s'" % (username,password))
    else:
        cursor.execute("grant all privileges on *.* to "+username+"@'%'"+" identified by '"+password+"'")
    cursor.close()
    conn.close()
    logger.info('adding mysql user succeed!')

def loadBalanceConf(master,masterpass,slave,logger):
    from cmd import scpCmd
    import os,shutil

    logger.info('configuring load balance ...')
    shutil.copy('../templateconfig/haproxy.cfg.template','../temp/haproxy.cfg')
    os.system('echo "        server s1 '+slave+':8088 cookie appinst1 check" >> ../temp/haproxy.cfg')
    src='../temp/haproxy.cfg'
    dest='/usr/local/haproxy/'
    r=scpCmd(master,src,dest,masterpass)
    os.remove(src)
    if r=='scp failed':
        logger.error('configuring load balance failed!')
        sys.exit(1)
    else:
        logger.debug(r)
        logger.info('configuring load balance succeed!')

def loadBalanceStart(master,masterpass,logger):
    from cmd import sshCmd
    logger.info('starting load balance ...')
    cmd='haproxy -f /usr/local/haproxy/haproxy.cfg'
    r=sshCmd(master,'root',masterpass,cmd)
    if r=='ssh failed':
        logger.error('starting load balance failed!')
        sys.exit(1)
    else:
        logger.debug(r)
        logger.info('starting load balance succeed!')

def nfsConf(master,masterpass,slave,logger):
    from cmd import scpCmd
    import os
    logger.info('configuring nfs ...')
    os.system('echo "/var/www/html '+slave+'(rw)" > ../temp/exports')
    src='../temp/exports'
    dest='/etc/'
    r=scpCmd(master,src,dest,masterpass)
    os.remove(src)
    if r=='scp failed':
        logger.error('configuring nfs failed!')
        sys.exit(1)
    else:
        logger.debug(r)
        logger.info('configuring nfs succeed!')

def scaleUpNfsConf(master, masterpass, slave, logger):
    from cmd import sshCmd
    logger.info('scale up ---> configuring nfs ...')
    cmd='echo " ' + slave + '(rw)" >> /etc/exports'
    r=sshCmd(master, 'root', masterpass, cmd)
    if r == 'ssh failed':
        logger.error('scale up ---> configuring nfs failed!')
        sys.exit(1)
    else:
        logger.debug(r)
        logger.info('scale up ---> configuring nfs succeed!')

def nfsExport(master,masterpass,logger):
    from cmd import sshCmd
    logger.info('nfs exportfs ...')
    cmd='exportfs -arv'
    r=sshCmd(master,'root',masterpass,cmd)
    if r=='ssh failed':
        logger.error('nfs exportfs failed!')
        sys.exit(1)
    else:
        logger.debug(r)
        logger.info('nfs exportfs succeed!')

def mountSlave(slave,slavepass,master,logger):
    from cmd import sshCmd
    logger.info('mounting nfs on slave ...')
    cmd='mount -t nfs -o bg '+master+':/var/www/html /var/www/html'
    logger.debug(cmd)
    r=sshCmd(slave,'root',slavepass,cmd)
    if r=='ssh failed':
        logger.error('mounting nfs on slave failed!')
        sys.exit(1)
    else:
        logger.debug(r)
        logger.info('mounting nfs on slave succeed!')

def fstabConf(slave,slavepass,master,logger):
    from cmd import scpCmd
    import os,shutil
    logger.info('configuring fstab ...')
    shutil.copy('../templateconfig/fstab.template','../temp/fstab')
    os.system('echo "'+master+':/var/www/html   /var/www/html   nfs rw,bg   0   0" >> ../temp/fstab')
    src='../temp/fstab'
    dest='/etc/'
    r=scpCmd(slave,src,dest,slavepass)
    os.remove(src)
    if r=='scp failed':
        logger.error('configuring fstab failed!')
        sys.exit(1)
    else:
        logger.debug(r)
        logger.info('configuring fstab succeed!')
